package com.app.controller;


import cn.hutool.core.codec.Base64;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.app.aop.LoginCheck;
import com.app.pojo.FileUploadData;
import com.app.pojo.MyPath;
import com.app.pojo.Status;
import com.app.service.impl.DiskService;
import com.app.utils.Judges;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;

@RestController
@RequestMapping("disk")
public class DiskController {
    private final DiskService diskService;

    public DiskController(DiskService diskService) {
        this.diskService = diskService;
    }

    @RequestMapping("test")
    @ResponseBody
    String test(){
        JSONObject jsonObject=new JSONObject();
        jsonObject.set("aaaaa","aslkdlko");
        jsonObject.set("acs123",123);
        return jsonObject.toString();
    }
    /*
        *******Parameters******
        guid: 0.029445724279864027
        id: WU_FILE_0
        name: 3-实习成果简介.doc
        type: application/msword
        lastModifiedDate: Mon Sep 20 2021 15:50:02 GMT+0800 (中国标准时间)
        size: 17408
        chunks: 2
        chunk: 1
        *******part******
        file
        3-实习成果简介.doc
        2048
        *************
     */
    @PostMapping("webUpload")
    @ResponseBody
    String uploadPart(FileUploadData fileUploadData){
        String userId="16328330937754942";
        int status;
        System.out.println(fileUploadData);
        status=diskService.uploadFile(userId,fileUploadData);
        System.out.println(status);
        return new JSONObject().set("status",status).toString();
    }

//    @LoginCheck
    @PostMapping("upload")
    @ResponseBody
    String upload(HttpServletRequest request,String userId,String path,String md5){
        userId="16328330937754942";
        int status = Status.INPUT_NOT_REGULATED;
        System.out.println("upload:"+path);
        try {
//            request.setCharacterEncoding("utf-8");
            Part part = request.getPart("file");
            status=diskService.uploadFile(userId,new MyPath(path),part);
//获取文件后缀名，即文件类型
            System.out.println(status);
        }catch (Exception e){
            e.printStackTrace();
        }
        JSONObject json=new JSONObject();
        json.set("ch","啊里路压");
        return json.set("status",status).toString();
    }
    //获取文件列表
    @LoginCheck
    @RequestMapping("list")
    @ResponseBody
    String fileList(HttpServletRequest request,String userId,String path){
        path=Base64.decodeStr(path);
        System.out.println("path = " + path);
        System.out.println("user: "+userId);
        if (Judges.judgePaths(path))
            return JSONUtil.toJsonStr(diskService.getFileList(userId,new MyPath(path)));
        else{
            JSONObject json = new JSONObject();
            json.set("status", Status.INPUT_NOT_REGULATED);
            return json.toString();
        }
    }

    /**
     * 多文件操作
     * @param sources 要操作文件列表
     * @param action move,copy,delete
     */
    @LoginCheck
    @RequestMapping("files")
    @ResponseBody
    String filesOp(HttpServletRequest request,String userId,@RequestParam(value = "sources[]") String[] sources, String target, String action){
        target=Base64.decodeStr(target);
        JSONObject json = new JSONObject();
        String res="";
        if ("delete".equals(action))
            res = diskService.delFiles(userId, null,sources).toString();
        else if ("move".equals(action))
            res = diskService.moveFiles(userId, sources, new MyPath(target));
        else if ("copy".equals(action))
            res = diskService.copyFiles(userId, sources,  new MyPath(target));
        return json.set("status",Status.SUCCESS).set("result",res).toString();

    }
    /**
     * 文件操作
     * @param source 要操作文件
     * @param action rename,move,copy,delete,mkdir  rename 和 move 性质一样
     */
    @LoginCheck
    @RequestMapping("file")
    @ResponseBody
    String fileOp(HttpServletRequest request,String userId,String source,String target,String action){
//        source=Base64.decodeStr(source);
//        target=Base64.decodeStr(target);
        int status=Status.INPUT_NOT_REGULATED;
        System.out.println(action);
        System.out.println(source+","+target);
        if ("rename".equals(action)||"move".equals(action))
            status=diskService.renameFile(userId,new MyPath(source),new MyPath(target));//移动重命名
        else if ("copy".equals(action))
            status=diskService.copyFile(userId,new MyPath(source),new MyPath(target));
        else if ("delete".equals(action))
            status=diskService.delFile(userId,new MyPath(source));
        else if ("mkdir".equals(action))
            status= diskService.mkDir(userId, new MyPath(source));

        System.out.println(status);
        JSONObject json = new JSONObject();
        json.set("status", status);
        return json.toString();
    }
}
